﻿using System.Reflection;

namespace _74_C_反射读写字段和属性
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.LoadFrom("TestClassLibrary.dll");
            Type type = assembly.GetType("TestClassLibrary.Student");
            //实例化对象
            Object obj = Activator.CreateInstance(type);
            FieldInfo fieldInfo = type.GetField("name");//普通字段
            fieldInfo.SetValue(obj, "张同学");
            Console.WriteLine(fieldInfo.GetValue(obj));

            FieldInfo fieldInfo1 = type.GetField("grade");//静态
            fieldInfo1.SetValue(null, "一年级");
            Console.WriteLine(fieldInfo1.GetValue(null));

            FieldInfo fieldInfo2 = type.GetField("age", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);//私有静态
            fieldInfo2.SetValue(obj, 12);//静态私有方法
            Console.WriteLine(fieldInfo2.GetValue(obj));

            PropertyInfo propertyInfo = type.GetProperty("ID");//普通属性
            propertyInfo.SetValue(obj, 111);
            Console.WriteLine(propertyInfo.GetValue(obj));

            PropertyInfo properInfo1 = type.GetProperty("Teacher");//静态属性
            properInfo1.SetValue(null, "test");
            Console.WriteLine(properInfo1.GetValue(null));

            PropertyInfo properInfo2 = type.GetProperty("Teacher", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);//静态私有属性
            properInfo1.SetValue(obj, "test");
            Console.WriteLine(properInfo1.GetValue(obj));
        }
    }
}
